#include <stateManipulator.h>
#include <iomanip>

using namespace std;


void stateManipulator::initProbabilityMap(int in)
{
  for(int i = 0; i < in; i++)
    {
      vector<double> temp;
      for(int j = 0; j < in; j++)
	{
	  if(i == j)
	    {
	      temp.push_back(1.0);
	    }
	  else
	    {
	      temp.push_back(0.0);
	    }
	}
      probabilityMap.push_back(temp);
    }
}

void stateManipulator::initAliasVector(int in)
{
  for(int i = 0; i < in; i++)
    {
      aliasVec.push_back(i);
    }
}

int stateManipulator::translateIndex(int index)
{
  int num = -1;
  for(uint i = 0; i < aliasVec.size(); i++)
    {
      if(index == aliasVec[i])
	{
	  num = i;
	}
    }
  return num;
}

void stateManipulator::setAliasVector(vector<int> set)
{
  if(aliasVec.size() > 0)
    aliasVec.clear();

  for(uint i = 0; i < set.size(); i++)
    {
      aliasVec.push_back(set[i]);
    }
}

void stateManipulator::showAliasVector()
{
  for(uint i = 0; i < aliasVec.size(); i++)
    {
      cout << aliasVec[i] << endl;
    }
}

void stateManipulator::setProbability(int from, int to, double probability)
{
  int translate_from;
  int translate_to;
  translate_from = translateIndex(from);
  translate_to = translateIndex(to);
  //probabilityMap[translate_from][translate_to] = probability;
  probabilityMap.at(translate_from).at(translate_to) = probability;
}

double stateManipulator::getProbability(int from, int to)
{
  int translate_from;
  int translate_to;
  translate_from = translateIndex(from);
  translate_to = translateIndex(to);
  //probabilityMap[translate_from][translate_to] = probability;
  return probabilityMap.at(translate_from).at(translate_to);
}

void stateManipulator::setProbabilityEach(int a, int b, double probability)
{
  int translate_a;
  int translate_b;
  translate_a = translateIndex(a);
  translate_b = translateIndex(b);
  probabilityMap[translate_a][translate_b] = probability;
  probabilityMap[translate_b][translate_a] = probability;
}

void stateManipulator::showProbabilityMap()
{
  for(uint i = 0; i < probabilityMap.size(); i++)
    {
      for(uint j = 0; j < probabilityMap[i].size(); j++)
	{
	  cout << setw(5) << probabilityMap[i][j] << " ";
	}
      cout << endl;
    }
}

void stateManipulator::setCurrentState(int state)
{
  currentState = state;
}

int stateManipulator::getCurrentState()
{
  return currentState;
}

bool stateManipulator::stateChenge(int to)
{
  int translate_to;
  int translate_current;
  translate_to = translateIndex(to);
  translate_current = translateIndex(currentState);

  if(getRandNum() < probabilityMap[translate_current][translate_to])
    {
      currentState = to;
      return true;
    }

  return false;
}

double stateManipulator::getRandNum()
{
  //return ran.FixedC();
  double randd;
  int randi = rand() % 10000;
  randd = (double)randi / 10000;

  return randd;
}
